using System.Linq;
using Shop.Backend.Api.Entity;
using Shop.Backend.Api.Params;
using Shop.Backend.Api.Repository;
using Shop.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Shop.Backend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    //这里的控制器是 ShoppingCarts 购物车
    public class ShoppingCartsController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Users> _usersRepository;
        private IRepository<Goods> _goodsRepository;
        private IRepository<Shops> _shopsRepository;
        private IRepository<ShoppingCarts> _shoppingCartsRepository;

        private TokenParameter _tokenParameter;


        public ShoppingCartsController(IConfiguration configuration
        , IRepository<Users> usersRepository
        , IRepository<Goods> goodsRepository
        , IRepository<Shops> shopsRepository
        , IRepository<ShoppingCarts> shoppingCartsRepository)
        {
            _configuration = configuration;
            _usersRepository = usersRepository;
            _goodsRepository = goodsRepository;
            _shopsRepository = shopsRepository;
            _shoppingCartsRepository = shoppingCartsRepository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        [HttpGet]
        public dynamic Get([FromQuery] QueryWithPager query)
        {
            // get请求默认从url中获取参数，如果需要使用实体接收参数，需要FromQuery特性
            var pageIndex = query.Pager.PageIndex;
            var pageSize = query.Pager.PageSize;

            var keyword = string.IsNullOrEmpty(query.Keyword) ? "" : query.Keyword.Trim();

            var shoppingCarts = _shoppingCartsRepository.Table.Where(x => x.IsDeleted == false); ;

            // 如果keyword不为空，则再去查询
            if (!string.IsNullOrEmpty(keyword))
            {
                shoppingCarts = shoppingCarts.Where(x => x.Id.ToString() == keyword
              || x.Number.ToString() == keyword
              || x.Remarks.Contains(keyword)); //未完善的模糊查询
            }
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = new
                {
                    Data = shoppingCarts,
                    Pager = new
                    {
                        pageIndex,
                        pageSize,
                        rowsTotal = shoppingCarts.Count()
                    }
                },
                Msg = "获取购物车列表成功^_^"
            });
        }

        [HttpGet("{id}")] //获取指定的购物车
        public dynamic Get(int id)
        {
            var shoppingCarts = _shoppingCartsRepository.Table.ToList().Where(x => x.FromUsersId == id)
            .Select(a =>
            {
                var Goods = _goodsRepository.Table.ToList().Where(x => x.Id == a.FromGoodsId).FirstOrDefault();

                return new
                {
                    GoodsImage = Goods.GoodsImage,
                    GoodsName = Goods.GoodsName,
                    NowPrice = Goods.NowPrice,
                    Number = a.Number,
                    Id = a.Id
                };
            })
            ;


            return new
            {
                Code = 1000,
                Data = shoppingCarts,
                Msg = "获取指定购物车成功^_^"
            };
        }

        //添加到购物车
        [HttpPost]
        public dynamic Post(CreateShoppingCart createShoppingCart)
        {
            var fromUsersId = createShoppingCart.FromUsersId;
            var fromShopsName = createShoppingCart.FromShopsName;
            var fromGoodsId = createShoppingCart.FromGoodsId;
            // var fromGoodsSKUId = createShoppingCart.FromGoodsSKUId;
            var number = createShoppingCart.Number;
            if (number > 99)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "你个穷逼，没钱还要添加这么多到购物车 你先把这99件结算了吧^_^"
                };
            }

            //找出商店的id
            var FromShopsId = _shopsRepository.Table.ToList().Where(x => x.ShopName == createShoppingCart.FromShopsName).FirstOrDefault();

            //添加到我的购物车
            var shoppingCart = new ShoppingCarts
            {
                FromUsersId = createShoppingCart.FromUsersId,
                FromShopsId = FromShopsId.Id,
                FromGoodsId = createShoppingCart.FromGoodsId,
                // FromGoodsSKUId = createShoppingCart.FromGoodsSKUId,
                Number = createShoppingCart.Number
            };

            _shoppingCartsRepository.Insert(shoppingCart);

            return new
            {
                Code = 1000,
                Data = shoppingCart,
                Msg = "添加到购物车成功^_^"
            };
        }


        //删除购物车
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
             _shoppingCartsRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "移除成功"
            };
        }

    }
}